(function($){

function checkEmail(id) {

	email = $(id).val();

	if ( /^[-\w]+(\.[-\w]+)*@[-\w]+(\.[-\w]+)*(\.[a-z]{2,3})$/i.test(email) ) {
		return true;
	} else {
		alert("Please input email.");
		$(id).focus();
		return false;
	}

}

function checkPassword(id) {

	password = $(id).val();

	if ( /^.{5,30}$/.test(password) ) {
		return true;
	} else {
		alert("Please input password. (5-30 chars)");
		$(id).focus();
		return false;
	}

}

function checkContent(id) {

	content = $(id).val();

	if ( /^.{5,5000}$/.test(content) ) {
		return true;
	} else {
		alert("Please input content. (5-500 chars)");
		$(id).focus();
		return false;
	}

}


function checkNickname(id) {

	nickname = $(id).val();

	if ( /^.{1,20}$/.test(nickname) ) {
		return true;
	} else {
		alert("Please input nickname. (1-20 chars)");
		$(id).focus();
		return false;
	}

}

function checkYear(id) {

	year = $(id).val();

	if ( /^(19[0-9]{2}|200[0-9])$/.test(year) ) {
		return true;
	} else {
		alert("Please input year of birth. (YYYY)");
		$(id).focus();
		return false;
	}

}

$(document).ready(function(){

	// highlight current topic name
	var topicList = $("#topiclist a");
	var foundMenu = false;
	for ( var i = 1, j = topicList.length; i < j ; i++ ) {
		// match current url to topic links
		if ( location.href.indexOf( topicList.eq(i).attr("href").substring(1) ) > -1 ) {
			foundMenu = true;
			topicList.eq(i).addClass("current");
		}
	}
	// no certern topic, highlight "All"
	if ( !foundMenu ) {
		topicList.eq(0).addClass("current");
	}

	
	// login form validation
	$("#login").submit(function(e){
	
		e.preventDefault();
		
		if ( checkEmail("#login-email") && checkPassword("#login-password") ) {
		
			$.post("login.php", $(this).serialize(), function(data) {
			
				if ( data == "OK" ) {
					location.reload(true);
				} else {
					alert("Email and/or password is not matched.");
				}
			
			} );
		
		}
		
	});

	
	// register form validation
	$("#register").submit(function(e){
	
		e.preventDefault();
		
		if ( checkEmail("#register-email")
			&& checkPassword("#register-password")
			&& checkNickname("#nickname")
			&& checkYear("#birth")
			) {
		
			$.post("register.php", $(this).serialize(), function(data) {
			
				if ( data == "OK" ) {
					location.reload(true);
				} else {
					alert(data);
				}
			
			} );
		
		}
		
	});
	
	// post new posts
	$("#newpost").submit(function(e){
	
		e.preventDefault();
		
		
		if ( checkContent("newpostcontent") ) {
		
			$.post("post.php", $(this).serialize(), function(data) {
			
				if ( data == "OK" ) {
					location.href = './?topic=' + $("#topic").val();
				} else {
					alert(data);
				}
			
			} );
		
		}
		
	
	});
	
	// post new posts
	$("#newcomment").submit(function(e){
	
		e.preventDefault();
		
		
		if ( checkContent("newcommentcontent") ) {
		
			$.post("comment.php", $(this).serialize()+"&postid="+$("a.pro").attr("postid") , function(data) {
			
				if ( data == "OK" ) {
					location.reload(true);
				} else {
					alert(data);
				}
			
			} );
		
		}
		
	
	});
	
	// votes
	$(".votebutton").click(function(e){
	
		e.preventDefault();
		
		$this = $(this);
		
		post_id = $this.attr("postid");
		
		if ( $this.hasClass("pro") ) {
			value = 1;
		} else {
			value = -1;
		}
		
		$.post("vote.php", ("postid="+ post_id + "&vote=" + value), function(data){
		
			if ( data == "OK" ) {
				location.reload(true);
			} else {
				alert(data);
			}
			
		});
	
	});
	
	//twitter
	$(".twitter").each(function(e){
			
		$this = $(this);
		
		$post = $this.closest(".post").find(".postarticle").text();
		
		$this.attr("href","http://twitter.com/share?url="+location.href+"&text="+$post);
		
	});


	//facebook
	$(".facebook").each(function(e){
			
		$this = $(this);
		
		$post = $this.closest(".post").find(".postarticle").text();
		
		$this.attr("href","http://www.facebook.com/sharer.php?u="+location.href+"&t="+$post);
		
	});

	//favourite buttons
	$(".favouriteButton").change(function(){
		
		post_id = $(this).attr("postid");
		
		if ( $(this).is(":checked") ) {
			checked = 1;
		} else {
			checked = 0;
		}
		
		$.post("favourite.php", ("postid=" + post_id + "&checked=" + checked), function(data) {

			if ( data != "OK" ) {
				alert(data);
			}		

		} );
		
	});
	
});

})(jQuery);